#ifndef __LUA_MODULE_HTTP_H__
#define __LUA_MODULE_HTTP_H__

#include "lua.h"
#include "cm_http.h"
#include "cm_fs.h"

typedef enum {
    HTTP_STATE_IDLE,
    HTTP_STATE_STARTED,
    HTTP_STATE_DONE
} lua_http_client_state_t;

// Structure to hold our HTTP client state
typedef struct {
    cm_httpclient_handle_t handle;
    lua_State *L;
    lua_http_client_state_t state;
    int self_ref; // Reference to the userdata object itself, to prevent garbage collection
    int on_done_ref;
    int on_error_ref;
    int on_progress_ref;
    // For in-memory responses
    char *response_buffer;
    size_t buffer_size;
    size_t total_len;
    // For file download responses
    int32_t file_handle;
    // For POST/PUT request body copy
    char *request_body_copy;
    // For request header copy
    char *request_header_copy;
} lua_http_client_t;

int luaopen_http(lua_State *L);
void cleanup_client_resources(lua_http_client_t *client);

#endif // __LUA_MODULE_HTTP_H__
